/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.Ability.BianQian;

import com.xcl.calculator.Dialog.MyToastDialog;
import com.xcl.calculator.Preference_RW;
import com.xcl.calculator.ResourceTable;
import com.xcl.calculator.XLog;
import com.xcl.calculator.slice.BianQian.first.BianQianShareAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.IBundleManager;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedhardware.devicemanager.DeviceInfo;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * The type Bian qian share ability.
 *
 * @author Xcl
 * @date 2022 /1/12
 * @package com.xcl.calculator.Ability.BianQian
 */
public class BianQianShareAbility extends Ability {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "BianQianShareAbility");
    /**
     * The Bian qian share ability slice.
     */
    private BianQianShareAbilitySlice BianQianShareAbilitySlice;

    /**
     * 启动
     *
     * @param intent the intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        try {
            XLog.pd_pd = Preference_RW.ff7_r();
            super.setAbilitySliceAnimator(null);
            super.setTransitionAnimation(0, 0);
            this.getWindow().addFlags(WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT);
            super.setMainRoute(BianQianShareAbilitySlice.class.getName());
            super.setAbilitySliceAnimator(null);
            super.setTransitionAnimation(0, 0);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 下方小弹窗
     *
     * @param text1 the text 1
     */
    private void ShowDialog(String text1) {
        try {
            ohos.data.distributed.device.DeviceInfo localDeviceInfo = KvManagerFactory.getInstance()
                    .createKvManager(new KvManagerConfig((this))).getLocalDeviceInfo();
            int localDeviceType = Integer.parseInt(localDeviceInfo.getType());
            if (localDeviceType == DeviceInfo.DeviceType.PHONE.value()) {
                new MyToastDialog(this.getContext(), text1, ResourceTable.Graphic_extoast_framem, 36, 25)
                        .setDuration(120)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0, 100)
                        .show();
                XLog.info(label, "当前设备类型为手机");
            } else if (localDeviceType == DeviceInfo.DeviceType.TABLET.value()) {
                new MyToastDialog(this.getContext(), text1, ResourceTable.Graphic_extoast_framet, 43, 37)
                        .setDuration(120)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setOffset(0, 100)
                        .show();
                XLog.info(label, "当前设备类型为平板");
            } else {
                XLog.warn(label, "暂不支持您的设备:(" + localDeviceInfo.getType() + ")");
                this.terminateAbility();
            }
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * 权限
     *
     * @param requestCode  the request code
     * @param permissions  the permissions
     * @param grantResults the grant results
     */
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 20220109: {
                // 匹配requestPermissions的requestCode
                if (grantResults.length > 0
                        && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    // 权限被授予之后做相应业务逻辑的处理
                } else {
                    // 权限被拒绝
                    this.ShowDialog("请进入系统设置进行授权后再使用此功能!");
                    /*无参--页面跳转开始*/
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(this.getBundleName())
                            .withAbilityName("com.xcl.calculator.Ability.BianQian.BianQianShowAbility")
                            .build();// 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    intent.setOperation(operation);// 把operation设置到intent中
                    this.startAbility(intent);
                    /*页面跳转结束--无参*/
                    /**
                     * 跳转到设置，我不想用
                     * Intent intent = new Intent();
                     *                     Operation operation = new Intent.OperationBuilder()
                     *                             .withAction(IntentConstants.ACTION_APPLICATION_DETAILS_SETTINGS)
                     *                             .withUri(Uri.getUriFromParts("package",getBundleName(),null))
                     *                             .build();
                     *                     intent.setOperation(operation);
                     *                     startAbility(intent);
                     */
                    this.onBackPressed();
                }
                return;
            }
        }
    }

    /**
     * 权限1
     *
     * @return main ability slice
     */
    public BianQianShareAbilitySlice getMainAbilitySlice() {
        return this.BianQianShareAbilitySlice;
    }

    /**
     * 权限2
     *
     * @param bianQianShareAbilitySlice the bian qian share ability slice
     */
    public void setBianQianAbilitySlice(BianQianShareAbilitySlice bianQianShareAbilitySlice) {
        this.BianQianShareAbilitySlice = bianQianShareAbilitySlice;
    }
}
